package com.zhoust.search.controller;

import com.zhoust.search.service.EsItemService;
import com.zhoust.utils.IMOOCJSONResult;
import com.zhoust.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author 15703
 * @version 1.0
 * @Title
 * @package com.zhoust.testcontroller
 * @date 2020/10/14 22:10
 * @desc
 */
@RestController
@RequestMapping("items")
public class ElasticsearchController {
    @Autowired
    private EsItemService esItemService;

    @GetMapping("/test")
    public String addString(){
        return "hello~ es";
    }

    @GetMapping("/search")
    public IMOOCJSONResult search(
             String keywords,
             String sort,
             Integer page,
             Integer pageSize) {

        if (StringUtils.isBlank(keywords)) {
            return IMOOCJSONResult.errorMsg("");
        }

        if (page == null) {
            page = 0;
        }

        if (pageSize == null) {
            pageSize = 10;
        }

        PagedGridResult grid = esItemService.searhItems(keywords,
                sort,
                page,
                pageSize);

        return IMOOCJSONResult.ok(grid);
    }


}
